#include "my_time.h"
#include <math.h>
#include <omp.h>

const int n = 3000000;
inline double f(double x) { return 4.0 / (pow(x, 2.0) + 1.0); }
const double a = 0.0;
const double b = 1.0;
const double h = (double)(b - a) / (double)n;

int main() {
  const int nthreads = 12;
  double mypi[nthreads];
  int tid = -1;

  MyTimeI;
  MyTimeA;

#pragma omp parallel num_threads(nthreads) private(tid) shared(a, h, mypi)
  {
    tid = omp_get_thread_num();
#pragma omp for
    for (int i = 1; i < n; i++) {
      mypi[tid] = mypi[tid] + f(a + i * h);
      // if(tid==0)printf("tid 0");
    }
  }
  for (int i = 1; i < nthreads; i++) {
    mypi[0] += mypi[i];
  }

  mypi[0] += (f(a) + f(b)) / 2.0;
  mypi[0] = h * mypi[0];

  MyTimeB;
  printf("my pi %15.14g", mypi[0]);

  return 0;
}
